<template>
  <el-dialog v-if="isShow" title="分配权限" :visible.sync="isShow">
    <el-tree
      ref="tree"
      :default-checked-keys="selected"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <div slot="footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      list: [],
      roleId: '',
      selected: []
    }
  },
  methods: {
    // 点击确定
    async btnOK() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds })
      this.$message.success('分配成功')
      this.isShow = false
    },

    // 获取所有权限点
    async initPermission() {
      const { data } = await getPermissionList()
      this.list = tranListToTreeData(data)
    },
    // 根据角色id获取权限点
    async getPermissonsById(id) {
      const { data } = await getRoleDetail(id)
      this.selected = data.permIds
      this.roleId = id
    }
  }
}
</script>
